package api

import (
	"goblog/models/dao"
	"github.com/astaxie/beego"
	"goblog/models"
	"encoding/json"
	"time"
)

type ArticleController struct {
	beego.Controller
	articleDao *dao.ArticleDao
}

func (c *ArticleController) Get() {
	if c.articleDao == nil {
		c.articleDao = new(dao.ArticleDao)
	}
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 10)
	title := c.GetString("title")
	term := new(dao.ArticleTerm)
	term.Size = size
	term.Page = page
	term.Title = title
	c.articleDao.Find(term)
	c.Data["json"] = models.JsonSuccess("", term);
	c.ServeJSON()
}

func (c *ArticleController) Post() {
	if c.articleDao == nil {
		c.articleDao = new(dao.ArticleDao)
	}
	var article models.Article
	json.Unmarshal(c.Ctx.Input.RequestBody, &article)
	article.Author = "huwhy"
	article.Status = dao.DISPLAY
	article.Updated = time.Now()
	article.Created = time.Now()
	c.articleDao.Save(&article)
	c.Data["json"] = models.JsonSuccess("成功", article.Id)
	c.ServeJSON()
}
